package com.neiquan.neiquandemo.test;

import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.ColorUtils;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.neiquan.neiquandemo.R;

import net.neiquan.applibrary.barlibrary.ImmersionBar;
import net.neiquan.applibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

/**
 * Created by geyifeng on 2017/7/19.
 */

public class ScrollTitleActivity extends BaseActivity {

    private ImmersionBar mImmersionBar;
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.rv)
    RecyclerView mRv;
    private OneAdapter mOneAdapter;
    private List<String> mItemList = new ArrayList<>();
    private int bannerHeight;

    @Override
    public void setViews() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.titleBar(R.id.toolbar).init();

        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        mRv.setLayoutManager(linearLayoutManager);
        mOneAdapter = new OneAdapter();
        mOneAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mRv.setAdapter(mOneAdapter);
        addHeaderView();
        mOneAdapter.setNewData(mItemList);
    }

    private void addHeaderView() {
        ImageView imageView = new ImageView(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.height=500;
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.mipmap.login);
        mOneAdapter.addHeaderView(imageView);
        ViewGroup.LayoutParams bannerParams = imageView.getLayoutParams();
        ViewGroup.LayoutParams titleBarParams = mToolbar.getLayoutParams();
        bannerHeight = bannerParams.height - titleBarParams.height - ImmersionBar.getStatusBarHeight(this);
    }

    protected void setListener() {
        mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            private int totalDy = 0;

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                totalDy += dy;
                if (totalDy <= bannerHeight) {
                    float alpha = (float) totalDy / bannerHeight;
                    mToolbar.setBackgroundColor(ColorUtils.blendARGB(Color.TRANSPARENT
                            , ContextCompat.getColor(ScrollTitleActivity.this, R.color.colorPrimary), alpha));
                } else {
                    mToolbar.setBackgroundColor(ColorUtils.blendARGB(Color.TRANSPARENT
                            , ContextCompat.getColor(ScrollTitleActivity.this, R.color.colorPrimary), 1));
                }
            }
        });
    }

    @Override
    public int getRootViewId() {
        return R.layout.activity_fragment;
    }

    @Override
    public void initData() {
        for (int i = 1; i <= 20; i++) {
            mItemList.add("item" + i);
        }
        setListener();
    }
}
